package com.yuesystem.config;

import com.zhenzi.sms.ZhenziSmsClient;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;




/**
 * 发送信息配置类.
 */
@Configuration
public class SmsConfig {
  @Value("${sms.apiUrl}")
  private String apiUrl;
  
  @Value("${sms.appId}")
  private String appId;
  
  @Value("${sms.appSecret}")
  private String appSecret;
  
  /**
   * 随机验证码.
   *
   * @return 验证码
   */
  public String printRandom() {
    // 取随机产生的验证码(4位数字)
    Random rnd = new Random();
    int randNum = rnd.nextInt(8999) + 1000;
    // 将整型数字转化成字符串
    String randStr = String.valueOf(randNum);
    return randStr;
  }
  
  /**
   * 发送信息.
   *
   * @param randNum 随机数
   * @param phoneNum 电话号码
   * @return 短信信息
   * @throws Exception 异常信息
   */
  public String sendMessage(String randNum, String phoneNum) throws Exception {
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("number", phoneNum);
    params.put("templateId", "10283");        // 短信模板ID，在个人中心的短信模板中查看
    String[] templateParams = new String[2];
    templateParams[0] = randNum;
    templateParams[1] = "5分钟";
    params.put("templateParams", templateParams);
    ZhenziSmsClient client = new ZhenziSmsClient(apiUrl, appId, appSecret);
    return client.send(params);
  }
}
